﻿/*
=============================================================================
Author:魏宝辉通过CodeSmithe生成
CreatedTime:2012-10-16
Description:Sms_Channel三层结构数据访问层
=============================================================================
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using FTchina.Models;//引入模型层
namespace FTchina.DAL
{
	public static partial class Sms_ChannelService
	{
		#region 增加一条数据
		public static int AddSms_Channel(Sms_Channel sms_Channel)
		{
			string sql =
			"INSERT INTO Sms_Channel(Sname,url,poststr,uname,upsw,lices,limit,encoding,SuccessString,Operators) "+
			"VALUES (@Sname,@url,@poststr,@uname,@upsw,@lices,@limit,@encoding,@SuccessString,@Operators)";
			
			 
				SqlParameter[] para = new SqlParameter[]
				{
						
					new SqlParameter("@Sname",sms_Channel.Sname),
						
					new SqlParameter("@url",sms_Channel.Url),
						
					new SqlParameter("@poststr",sms_Channel.Poststr),
						
					new SqlParameter("@uname",sms_Channel.Uname),
						
					new SqlParameter("@upsw",sms_Channel.Upsw),
						
					new SqlParameter("@lices",sms_Channel.Lices),
						
					new SqlParameter("@limit",sms_Channel.Limit),
						
					new SqlParameter("@encoding",sms_Channel.Encoding),
						
					new SqlParameter("@SuccessString",sms_Channel.SuccessString),
						
					new SqlParameter("@Operators",sms_Channel.Operators)
				};
				int newId = DBHelper.ExecuteCommand(sql, para);
				return newId;
			 
		}
		#endregion
		#region 删除数据以主键，标识列，唯一列构建的删除
			
		public static int DeleteSms_ChannelById(long id)	
		{
			string sql="DELETE "+"Sms_Channel"+" Where id=@id";
			 SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@id", id)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}		
		
		#endregion
		#region 修改数据
		//考虑标识列与主键不一致情形
		//以标识列修改实体
		public static int ModifySms_ChannelByid(Sms_Channel sms_Channel)
		{
		string sql ="UPDATE "+"Sms_Channel"+" set "+
					"Sname=@Sname, "+
					"url=@url, "+
					"poststr=@poststr, "+
					"uname=@uname, "+
					"upsw=@upsw, "+
					"lices=@lices, "+
					"limit=@limit, "+
					"encoding=@encoding, "+
					"SuccessString=@SuccessString, "+
					"Operators=@Operators "+ 
				" Where id=@id";
			 
				SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@id",sms_Channel.Id),
					new SqlParameter("@Sname",sms_Channel.Sname),
					new SqlParameter("@url",sms_Channel.Url),
					new SqlParameter("@poststr",sms_Channel.Poststr),
					new SqlParameter("@uname",sms_Channel.Uname),
					new SqlParameter("@upsw",sms_Channel.Upsw),
					new SqlParameter("@lices",sms_Channel.Lices),
					new SqlParameter("@limit",sms_Channel.Limit),
					new SqlParameter("@encoding",sms_Channel.Encoding),
					new SqlParameter("@SuccessString",sms_Channel.SuccessString),
					new SqlParameter("@Operators",sms_Channel.Operators)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}					
		#endregion
		#region 查询数据
		public static IList<Sms_Channel> GetAllSms_Channels()
		{
			string sqlAll = "SELECT * FROM Sms_Channel";
			return GetSms_ChannelsBySql(sqlAll);
		}
		//以标识列获得对象
		public static Sms_Channel GetSms_ChannelByid(object id)
		{
			string sql = "SELECT * FROM "+"Sms_Channel"+" WHERE id = @id";
			
            using (SqlDataReader reader = DBHelper.GetReader(sql, new SqlParameter("@id", id)))
			{
				 
				if (reader.Read())
				{
					Sms_Channel sms_Channel = new Sms_Channel();
					sms_Channel.Id=Convert.ToInt32(reader["id"]);
					sms_Channel.Sname=Convert.ToString(reader["Sname"]);
					sms_Channel.Url=Convert.ToString(reader["url"]);
					sms_Channel.Poststr=Convert.ToString(reader["poststr"]);
					sms_Channel.Uname=Convert.ToString(reader["uname"]);
					sms_Channel.Upsw=Convert.ToString(reader["upsw"]);
					sms_Channel.Lices=Convert.ToString(reader["lices"]);
					sms_Channel.Limit=Convert.ToInt32(reader["limit"]);
					sms_Channel.Encoding=Convert.ToString(reader["encoding"]);
					sms_Channel.SuccessString=Convert.ToString(reader["SuccessString"]);
					sms_Channel.Operators=Convert.ToString(reader["Operators"]);
					reader.Close();
					
					return sms_Channel;
					}
				else
				{
					reader.Close();
					return null;
				}
			}
			 
		}
					
		//私有方法供构建好的SQL语句调用
		private static IList<Sms_Channel> GetSms_ChannelsBySql(string safeSql)
		{
			List<Sms_Channel> list = new List<Sms_Channel>();
			 
            using (DataTable table = DBHelper.GetDataTable(safeSql))
			{
				foreach (DataRow row in table.Rows)
				{
					Sms_Channel sms_Channel = new Sms_Channel();
					sms_Channel.Id=Convert.ToInt32(row["id"]);
					sms_Channel.Sname=Convert.ToString(row["Sname"]);
					sms_Channel.Url=Convert.ToString(row["url"]);
					sms_Channel.Poststr=Convert.ToString(row["poststr"]);
					sms_Channel.Uname=Convert.ToString(row["uname"]);
					sms_Channel.Upsw=Convert.ToString(row["upsw"]);
					sms_Channel.Lices=Convert.ToString(row["lices"]);
					sms_Channel.Limit=Convert.ToInt32(row["limit"]);
					sms_Channel.Encoding=Convert.ToString(row["encoding"]);
					sms_Channel.SuccessString=Convert.ToString(row["SuccessString"]);
					sms_Channel.Operators=Convert.ToString(row["Operators"]);
					list.Add(sms_Channel);
				}
				return list;
			}
             
		}
		private static IList<Sms_Channel> GetSms_ChannelsBySql( string sql, params SqlParameter[] values )
		{
			List<Sms_Channel> list = new List<Sms_Channel>();
			 
            using (DataTable table = DBHelper.GetDataTable(sql, values))
			{
				foreach (DataRow row in table.Rows)
				{
					Sms_Channel sms_Channel = new Sms_Channel();
					sms_Channel.Id=Convert.ToInt32(row["id"]);
					sms_Channel.Sname=Convert.ToString(row["Sname"]);
					sms_Channel.Url=Convert.ToString(row["url"]);
					sms_Channel.Poststr=Convert.ToString(row["poststr"]);
					sms_Channel.Uname=Convert.ToString(row["uname"]);
					sms_Channel.Upsw=Convert.ToString(row["upsw"]);
					sms_Channel.Lices=Convert.ToString(row["lices"]);
					sms_Channel.Limit=Convert.ToInt32(row["limit"]);
					sms_Channel.Encoding=Convert.ToString(row["encoding"]);
					sms_Channel.SuccessString=Convert.ToString(row["SuccessString"]);
					sms_Channel.Operators=Convert.ToString(row["Operators"]);
					list.Add(sms_Channel);
				}
				return list;
			}
            
			 
		}
		#endregion
		#region 分页的方法page为当前页码，NUMCount为每页数据
		public static IList<Sms_Channel> ShowSms_ChannelForPage(int curragePage,int NUMCount)
		{
			int number = 0;
			if (curragePage > 1) {
				number = NUMCount * (curragePage - 1);
			}
			string strsql= "select top "
						   +NUMCount+" * from Sms_Channel where  id not in ( select top "
						   +number+" id from Sms_Channel order by id desc ) order by id desc";
			return GetSms_ChannelsBySql(strsql);
		}
		#endregion
	}
}

